<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>SSM框架整合</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../elementui/index.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>
            <small>账户管理</small>
        </h1>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="账户名称" align="center"></el-table-column>
                <el-table-column prop="money" label="金额" align="center"></el-table-column>

                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增账户" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" label-position="right" label-width="100px">


                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="账户名称">
                                    <el-input v-model="formData.name" type="text"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="金额">
                                    <el-input v-model="formData.money" type="text"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑账户" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataAddForm" :model="formData" label-position="right" label-width="100px">


                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="账户名称">
                                    <el-input v-model="formData.name" type="text"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="金额">
                                    <el-input v-model="formData.money" type="text"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../elementui/index.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            dataList: [],
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false//编辑表单是否可见
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            findPage() {
                //通过get请求发送Ajax给服务器, 获取所有的账户信息
                axios.get('/account/findAll.do')
                    .then(res => {
                        this.dataList = res.data;
                    }).catch(err => {
                    console.log(err)
                })
            },
            handleCreate() {
                this.dialogFormVisible = true;
                this.formData = {};
            },
            handleAdd() {
                //1.发送ajax给服务器, 实现添加账户的功能
                axios.post("/account/save.do", this.formData).then(
                    res => {
                        //2.在ajax回调方法中, 关闭新增窗口, 重新查询所有账户显示在页面
                        this.dialogFormVisible = false;
                        this.dataList = res.data;
                    }
                ).catch(
                    error => {
                        console.dir(error);
                    }
                );
            },
            handleDelete(row) {
                let id = row.id;
                axios.get("/account/delete.do?id=" + id)
                    .then(res => {
                        if (res.data == "delete success") {
                            this.$message(row.name + res.data);
                            this.findPage();
                        }
                    }).catch(err => {
                    console.log(err);
                })
            },
            handleUpdate(row) {
                this.dialogFormVisible4Edit = true;
                this.formData = row;
            },
            handleEdit() {
                axios.post("/account/update.do", this.formData)
                    .then(res => {
                        if (res.data == "update success") {
                            this.dialogFormVisible4Edit = false;
                            this.findPage();
                        }
                    }).catch(err => {
                    console.log(err);
                })
            }
        }
    })
</script>
</html>